import { apiClient } from './axios'

// 系统配置接口
export interface SystemConfig {
  id: number
  config_key: string
  config_value: string
  config_type: 'string' | 'number' | 'boolean' | 'json'
  category: string
  description?: string
  is_public: boolean
  is_editable: boolean
  created_at: string
  updated_at: string
}

// 系统字典接口
export interface SystemDictionary {
  id: number
  dict_type: string
  dict_key: string
  dict_value: string
  dict_label: string
  sort_order: number
  is_active: boolean
  parent_id?: number
  description?: string
  created_at: string
  updated_at: string
  children?: SystemDictionary[]
}

export const systemConfigApi = {
  // 获取所有系统配置
  getSystemConfigs: (category?: string) => {
    const params = category ? { category } : {}
    return apiClient.get<SystemConfig[]>('/system-configs', { params })
  },

  // 获取系统配置详情
  getSystemConfig: (id: number) => {
    return apiClient.get<SystemConfig>(`/system-configs/${id}`)
  },

  // 根据key获取配置
  getSystemConfigByKey: (key: string) => {
    return apiClient.get<SystemConfig>(`/system-configs/key/${key}`)
  },

  // 创建系统配置
  createSystemConfig: (data: Omit<SystemConfig, 'id' | 'created_at' | 'updated_at'>) => {
    return apiClient.post<SystemConfig>('/system-configs', data)
  },

  // 更新系统配置
  updateSystemConfig: (id: number, data: Partial<SystemConfig>) => {
    return apiClient.put<SystemConfig>(`/system-configs/${id}`, data)
  },

  // 批量更新系统配置
  batchUpdateSystemConfigs: (configs: Array<{ id: number; config_value: string }>) => {
    return apiClient.put('/system-configs/batch', { configs })
  },

  // 删除系统配置
  deleteSystemConfig: (id: number) => {
    return apiClient.delete(`/system-configs/${id}`)
  },

  // 获取配置分类列表
  getConfigCategories: () => {
    return apiClient.get<string[]>('/system-configs/categories')
  },

  // 获取所有系统字典
  getSystemDictionaries: (dictType?: string) => {
    const params = dictType ? { dict_type: dictType } : {}
    return apiClient.get<SystemDictionary[]>('/system-dictionaries', { params })
  },

  // 获取字典树形结构
  getSystemDictionaryTree: (dictType: string) => {
    return apiClient.get<SystemDictionary[]>(`/system-dictionaries/tree/${dictType}`)
  },

  // 获取系统字典详情
  getSystemDictionary: (id: number) => {
    return apiClient.get<SystemDictionary>(`/system-dictionaries/${id}`)
  },

  // 创建系统字典
  createSystemDictionary: (data: Omit<SystemDictionary, 'id' | 'created_at' | 'updated_at' | 'children'>) => {
    return apiClient.post<SystemDictionary>('/system-dictionaries', data)
  },

  // 更新系统字典
  updateSystemDictionary: (id: number, data: Partial<SystemDictionary>) => {
    return apiClient.put<SystemDictionary>(`/system-dictionaries/${id}`, data)
  },

  // 删除系统字典
  deleteSystemDictionary: (id: number) => {
    return apiClient.delete(`/system-dictionaries/${id}`)
  },

  // 获取字典类型列表
  getDictionaryTypes: () => {
    return apiClient.get<string[]>('/system-dictionaries/types')
  },

  // 根据字典类型和key获取字典值
  getDictionaryValue: (dictType: string, dictKey: string) => {
    return apiClient.get<SystemDictionary>(`/system-dictionaries/value/${dictType}/${dictKey}`)
  }
};